'use strict'

define([ 'jquery', 'bootstrap', 'select2', 'shiftform', 'validate',
    'validate.en', 'datetimepicker', 'countdown'], function($) {
  return {
    init : function() {
      this.duringTime = 10*60;
      this.initPage();
      this.countDownTime();
      this.switchOption();
    },
      
    initPage : function() {
      $(".overflow-height").css("height", $(document).height() - 680);
      
      $('.practice-option').click(function() {
        var that = this
        
        var qualify = $(this).find("div").data("qualify");
        var score = $(this).find("div").data("option");
        $(this).parent("tr").find(".score").empty().append("<span>" + score + "</span>");
        $(this).addClass("color-level").siblings().removeClass("color-level");
        
        var qualifyName = 'qualify' + qualify + 'Score';
        $('input[name="' + qualifyName + '"]').val(score);
      
      })
    },
    
    countDownTime: function() {
      var that = this;
      var minute = that.duringTime;
      
      $(".countdown-timer").flipcountdown({
        tick: function() {
          var minutes, seconds;
          var countMinute = minute;
          
          minutes = parseInt(countMinute/60) > 9 ? parseInt(countMinute/60) : '0'+ parseInt(countMinute/60)
          seconds = parseInt(countMinute%60) > 9 ? parseInt(countMinute%60) : '0'+ parseInt(countMinute%60)
          
          if(countMinute == 0) {
            $(".countdown-timer").remove();
          }
          
          countMinute = minute--;
          return minutes +" "+ seconds
        }
      });
    },
    
    switchOption: function() {
      $(document).on("click", ".abilities dd", function() {
        $(this).addClass("active").siblings().removeClass("active")
        
        var currentIndex = $(this).data("option") - 1;
        var optionHeight =  currentIndex < 0 ? 0 : $(".assessment-wrap .option").eq(currentIndex).height();
        $(".assessment-wrap").animate({
          scrollTop: optionHeight
        }, 1000)
      })
    },
  }
})